<template>
  <div class="common-layout">
    <el-container>
      <el-header class="header-wrapper">
        <div class="header-content">
          <h3>通用后台管理系统</h3>
          <span class="welcome">欢迎您的登录</span>
        </div>
      </el-header>
      <el-main>
        <div class="login-wrapper">
          <el-card class="box-card">
            <el-tabs v-model="activeTab" stretch>
              <el-tab-pane label="登录" name="login">
                <el-form class="login-form" :model="loginData">
                  <el-form-item prop="account" label="账号">
                    <el-input v-model="loginData.account" placeholder="请输入账号"></el-input>
                  </el-form-item>
                  <el-form-item prop="password" label="密码">
                    <el-input v-model="loginData.password" placeholder="请输入密码"></el-input>
                  </el-form-item>
                  <div class="footer-wrapper">
                    <div class="forget-password">
                      <span class="forget-password-button" @click="openForget">忘记密码</span>
                    </div>
                    <div class="footer-button">
                      <el-button type="primary" @click="handleLogin">登录</el-button>
                    </div>
                    <div class="footer-go-register">
                      还没有账号？<span class="go-register">马上注册</span>
                    </div>
                  </div>
                </el-form>
              </el-tab-pane>
              <el-tab-pane label="注册" name="register">
                <el-form class="login-form" :model="registerData" label-width="auto">
                  <el-form-item prop="account" label="账号">
                    <el-input v-model="registerData.account" placeholder="账号长度6-12位"></el-input>
                  </el-form-item>
                  <el-form-item prop="password" label="密码">
                    <el-input v-model="registerData.password" placeholder="密码需长度6-12位含字母数字"></el-input>
                  </el-form-item>
                  <el-form-item prop="password" label="确认密码">
                    <el-input v-model="registerData.repassword" placeholder="请再次输入密码"></el-input>
                  </el-form-item>
                  <div class="footer-button">
                    <el-button type="primary" @click="handleRegister">注册</el-button>
                  </div>
                </el-form>
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </div>
      </el-main>
      <el-footer class="footer-wrapper">
        <div class="footer-content"></div>
      </el-footer>
    </el-container>
  </div>

  <ForgetPassword ref="forgetRef"></ForgetPassword>
</template>

<script setup lang='ts'>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useUserInfo } from '@/stores/userInfo'
import { login, register } from '@/api/login'
import ForgetPassword from './components/ForgetPassword.vue'

defineOptions({
  name: 'Login'
})

const store = useUserInfo()
const router = useRouter()
interface formData {
  account: string;
  password: string;
  repassword?: string;
}
const loginData: formData = reactive({
  account: '',
  password: '',
})
const registerData: formData = reactive({
  account: '',
  password: '',
  repassword: '',
})
const forgetRef = ref()
const activeTab = ref('login')

const openForget = () => {
  forgetRef.value.open()
}

const handleLogin = async () => {
  const { message, data } = await login(loginData)
  
  ElMessage.success(message)
  localStorage.setItem('token', data.token)
  localStorage.setItem('id', data.user.id)
  store.userInfo(data.user.id)
  router.push('/home')
}

const handleRegister = async () => {
  if(registerData.password == registerData.repassword){
    const { message } = await register(registerData)
    ElMessage.success(message)
    activeTab.value = 'login'
  } else {
    ElMessage.error('两次密码不一致')
  }
}
</script>

<style lang="scss" scoped>
.header-wrapper {
  .header-content {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .welcome {
      font-size: 14px;
    }
  }
}

.el-main {
  background-image: url('@/assets/code.jpg');
  height: 600px;
  --el-main-padding: 0;

  .login-wrapper {
    width: 1200px;
    height: 600px;
    margin: 0 auto;

    .box-card {
      width: 350px;
      height: 375px;
      float: right;
      position: relative;
      top: 14%;

      .login-form {
        .footer-wrapper {
          display: flex;
          flex-direction: column;

          .forget-password {
            display: flex;
            justify-content: flex-end;
            margin: 10px 0;

            .forget-password-button {
              font-size: 12px;
              color: #409eff;
              cursor: pointer;
            }
          }

          .footer-go-register {
            font-size: 12px;
            margin: 12px 0;
            display: flex;
            justify-content: center;

            .go-register {
              font-size: 12px;
              color: #409eff;
              cursor: pointer;
            }
          }
        }
        .footer-button {
          width: 100%;
          display: flex;
          justify-content: center;
        }
      }
    }
  }
}

.el-form {
  margin-top: 30px;
}

:deep(.el-tabs__item) {
  color: #333;
  font-size: 18px;
}

:deep(.el-input__inner){
  height: 40px;
}

:deep(.el-form-item__label){
  height: 40px;
  line-height: 40px;
}

:deep(.el-button){
  width: 300px;
  height: 45px;
  font-size: 16px;
}
</style>